import cv2

capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while capture.isOpened():
    retval, img = capture.read()
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    if retval:
        cv2.imshow('video', img)
        cv2.imshow('video_gray', img_gray)

    key = cv2.waitKey(1)
    # 按下空格键退出
    if key == 32:
        break

capture.release()
cv2.destroyAllWindows()
